package com.zwx.bingo.abstraction.endpoint;


import com.zwx.bingo.data.interfaces.IRawService;
import com.zwx.bingo.loggable.annotation.Loggable;
import com.zwx.bingo.stateful.Responses;
import com.zwx.bingo.stateful.body.StatefulBody;
import com.zwx.bingo.stateful.body.impl.SuccessResponseBody;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.io.Serializable;

@FunctionalInterface
public interface CreationEndpoint<ENTITY extends Serializable, ID extends Serializable> {

    @Autowired
    public abstract IRawService<ENTITY, ID> getRawService();


    public default void beforeCreate(ENTITY entity) {
        //in abstract method, there's nothing to do
        //implement this method if in demand
    }

    public default void afterCreated(ENTITY entity) {
        //in abstract method, there's nothing to do
        //implement this method if in demand
    }

    //TODO 添加权限点控制
    @Loggable
    @PostMapping(value = "/create", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    @ApiOperation(value = "Create")
    public default ResponseEntity<StatefulBody<ENTITY>> create(@RequestBody ENTITY entity) {

        beforeCreate(entity);
        ENTITY created = getRawService().save(entity);
        afterCreated(created);
        return Responses.created(SuccessResponseBody.builder()
                .payload(created).build());
    }
}
